/*
 * ConnPool.h
 *
 *  Created on: Sep 18, 2016
 *      Author: root
 */

#ifndef BASE_DATABASE_YCONNPOOL_H_
#define BASE_DATABASE_YCONNPOOL_H_
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/exception.h>
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/statement.h>
#include <pthread.h>
#include <list>
#include <string>
using namespace std;
using namespace sql;
class ConnPool {
private:
    int curSize; //当前已建立的数据库连接数量
    int maxSize; //连接池中定义的最大数据库连接数
    string username;
    string password;
    string url;
    list<Connection*> connList; //连接池的容器队列
    pthread_mutex_t lock; //线程锁
    static ConnPool *connPool;
    Driver*driver;
    Connection*createConnection(); //创建一个连接
    void initConnection(int iInitialSize); //初始化数据库连接池
    void destoryConnection(Connection *conn); //销毁数据库连接对象
    void destoryConnPool(); //销毁数据库连接池
public:
    ConnPool(string url, string user, string password, int maxSize); //构造方法
    ~ConnPool();
    Connection*getConnection(); //获得数据库连接
    void releaseConnection(Connection *conn); //将数据库连接放回到连接池的容器中
};

#endif /* BASE_DATABASE_YCONNPOOL_H_ */
